let storage = bui.storage();
let org;
let language;
let translate;
loader.define(function (require, exports, module, global) {
    let pageview = {
        init() {
            translate = global.translate
            language = storage.get("language", 0)
            if (!language) {
                language = 'chinese_simplified'
            }

            try {
                translate.selectLanguageTag.show = false;
                translate.ignore.id.push('languageContent')
                let curLanguage = translate.language.getCurrent()
                if (curLanguage !== language) {
                    translate.changeLanguage(language)
                }
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }
            let languageArr = storage.get('languageArr',0)
            let html = ''
            let html2 = '<select class="languageSelect" id="languageSelectId">\n'
            if (language !== 'chinese_simplified' && language !== 'english') {
                languageArr.forEach(lan => {
                    if (lan.id === language) {
                        html += '<span class="language" id="'+lan.id+'">'+lan.name+'</span>|'
                    } else {
                        html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                    }
                })
            } else {
                languageArr.forEach(lan => {
                    html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                })
            }
            html+= '<span class="language" id="english">English</span>|<span class="language" id="chinese_simplified">简体中文</span>'
            router.$("#languageContent").html(html + html2 + "</select>")

            router.$(".language").click(function () {
                let selectId = $(this).attr('id')
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })
            router.$("#languageSelectId").val("");
            router.$("#languageSelectId").change(function () {
                let selectId = $(this).val()
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })

            let uiLoading = bui.loading({
                appendTo:"#dashboardLoading",
                width: 40,
                height: 40,
                opacity: 0.5,
                text:"加载中。。。",
                autoClose: false,
                callback: function (argument) {
                }
            });

            let token = storage.get("token",0)
            let id;

            router.$("#dashFh").click(function () {
                storage.set("tzPage", '0')
                router.replace({url: "pages/mainIndex/mainIndex.html"});
            })

            if (token) {
                // uiLoading.show()
                bui.ajax({
                    url: global.api.verifyToken,
                    method: "POST",
                    headers: {Token: token}
                }).then(function(res){
                    if (res.code != '200') {
                        uiLoading.stop()
                        storage.set("dlTz", '18')
                        router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                    }
                })


                let uiList = bui.list({
                    id: "#userScrollList",
                    url: global.api.getUserListByUV,
                    pageSize: 10, // 当pageSize 小于返回的数据大小的时候,则认为是最后一页,接口返回的数据最好能返回空数组,而不是null
                    data: {},
                    headers: {Token: token},
                    //如果分页的字段名不一样,通过field重新定义
                    field: {
                        page: "page",
                        size: "limit",
                        data: "data"
                    },
                    template: function(data) {
                        let html = "";
                        data.forEach(function(el, index) {
                            let statusStr = ''
                            // html += '<li class="bui-btn">\n' +
                            //     '<div class="bui-box">'+
                            //     '                <div class="bui-thumbnail" style="width: 1.5rem;height: 1.5rem"><img src="'+el.avatar+'" alt=""></div>\n' +
                            //     '                <div class="span1">\n' +
                            //     '                    <h3 class="item-title">'+el.nickName+'</h3>\n'
                            html += '<li class="bui-btn">\n' +
                                '<div class="bui-box">'+
                                '                <div class="span1">\n' +
                                '                    <h3 class="item-title">'+el.nickName+'</h3>\n'+
                                '                </div>\n'+
                                '</div>\n'+
                                '<div class="bui-box">'+
                                '                <div class="span1">\n' +
                                '                    日发帖数：<h3 class="item-title">'+el.ftNum+'</h3>\n'+
                                '                </div>\n'+
                                '                <div class="span1">\n' +
                                '                    日用户浏览量：<h3 class="item-title">'+el.uvNum+'</h3>\n'+
                                '                </div>\n'+
                                '                <div class="span1">\n' +
                                '                    日点击量：<h3 class="item-title">'+el.pvNum+'</h3>\n'+
                                '                </div>\n'+
                                '</div>\n'+
                                '</li>\n'
                        });
                        uiLoading.stop()

                        setTimeout(function () {
                            try {
                                translate.execute();
                            }catch (e) {
                                console.log("切换语言报错")
                            }
                        },1000)
                        return html;
                    }
                })
            } else {
                bui.confirm({
                    "title": "",
                    "height": 460,
                    "content": '<div class="bui-box-center"><div><h3>当前未登录，请登录</h3></div></div>',
                    "buttons": [{ name: "登录", className: "primary-reverse" }],
                    callback:function(){
                        this.close();
                        storage.set("dlTz", '18')
                        router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                    }
                });
            }
        }
    };

    // 初始化
    pageview.init();

    // 输出模块
    return pageview;
})
